package com.hourse.platform.service.impl;

import com.hourse.platform.common.ErrorMsg;
import com.hourse.platform.dao.BaseDao;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.service.PublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

@Service
public class PublicServiceImpl implements PublicService {

  @Autowired
  protected BaseDao baseDao;

  @Transactional
  @Override
  public Object findById(String id, Class clazz) {
    return getObjectById(id, clazz);
  }

  /**
   * 保存或更新
   *
   * @param obj
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public void saveOrUpdate(Object obj) throws AppException {
    try {
      baseDao.saveOrUpdate(obj);
    }catch (Exception e){
      throw ErrorMsg.DB_ERR.Exception();
    }
  }

  /**
   * 保存
   *
   * @param obj
   * @throws AppException
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public void save(Object obj) throws AppException {
    try {
      baseDao.saveObject(obj);
    }catch (Exception e){
      throw ErrorMsg.DB_ERR.Exception();
    }
  }

  /**
   * @功能描述: 【删除信息】
   */
  @Transactional(rollbackFor = AppException.class)
  @Override
  public void delete(Serializable id, Class clazz) throws AppException {
    try {
      baseDao.deleteObjectSerializableKey(clazz,id);
    }catch (Exception e){
      throw ErrorMsg.DB_ERR.Exception();
    }
  }

  /**
   * @param id
   * @param clazz
   * @功能描述: 【获取对象】
   */
  @Transactional(readOnly = true)
  @Override
  public Object getObjectById(Serializable id, Class clazz) {
    return baseDao.getObjectBySerializableKey(clazz,id);
  }
}
